package com.ren.article.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.article.entity.ReclArticleCollection;
import com.ren.article.entity.ReclArticleComment;
import com.ren.article.entity.ReclArticlePraise;
import com.ren.article.entity.vo.ReclArticleQueryVo;
import com.ren.article.entity.vo.ReclArticleVo;
import com.ren.article.service.ReclArticleCollectionService;
import com.ren.article.service.ReclArticleCommentService;
import com.ren.article.service.ReclArticlePraiseService;
import com.ren.article.service.ReclArticleService;
import com.ren.utils.page.PageItem;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ReclArticleFrontController
 * @Description: TODO
 * @Author: RZY
 * @DATE: 2022/7/21 9:58
 * @Version: v1.0
 */


@Api(tags = "美文前台接口")
@RestController
@RequestMapping("/recl-article/front")
public class ReclArticleFrontController {

    @Autowired
    ReclArticleService articleService;

    @Autowired
    ReclArticlePraiseService articlePraiseService;

    @Autowired
    ReclArticleCommentService articleCommentService;

    @Autowired
    ReclArticleCollectionService articleCollectionService;

    @ApiOperation(value = "查询指定用户所有收藏的美文")
    @GetMapping("/article-detail-collection/{userId}")
    public R getArticleByCollectionUserId(@PathVariable Long userId) {
        List<ReclArticleVo> reclArticles = articleService.getArticleByCollectionUserId(userId);
        return R.ok()
                .data("reclArticles", reclArticles);
    }

    @ApiOperation(value = "查询前台美文数据分页")
    @PostMapping("/{current}/{limit}")
    public R getFrontArticleOfPage(@RequestBody(required = false) ReclArticleQueryVo reclArticleQueryVo,
                                 @PathVariable long current,
                                 @PathVariable long limit) {
        PageItem<ReclArticleVo> articlePageItem = articleService.getFrontArticleOfPage(reclArticleQueryVo, current, limit);
        return R.ok()
                .data("records", articlePageItem.getPageRecords())
                .data("total", articlePageItem.getTotal());
    }

    @ApiOperation(value = "添加美文点赞")
    @PostMapping("/article-praise")
    public R addArticlePraise(@RequestBody ReclArticlePraise reclArticlePraise) {
        boolean res = articlePraiseService.saveOrUpdate(reclArticlePraise);
        if(res) return R.ok()
                .message("感谢你的支持！");
        else return R.error()
                .message("点赞失败，请稍后再试！");
    }

    @ApiOperation(value = "添加美文评论")
    @PostMapping("/article-comment")
    public R addArticleComment(@RequestBody ReclArticleComment reclArticleComment) {
        boolean res = articleCommentService.saveOrUpdate(reclArticleComment);
        if(res) return R.ok()
                .message("评论成功！");
        else return R.error()
                .message("评论失败，请稍后再试！");
    }

    @ApiOperation(value = "添加美文收藏")
    @PostMapping("/article-collection")
    public R addArticleCollection(@RequestBody ReclArticleCollection reclArticleCollection) {
        boolean res = articleCollectionService.saveOrUpdate(reclArticleCollection);
        if(res) return R.ok()
                .message("感谢你的支持！");
        else return R.error()
                .message("收藏失败，请稍后再试！");
    }

    @ApiOperation(value = "查询指定美文的所有评论")
    @PostMapping("/article-comment/{articleId}")
    public R addArticleComment(@PathVariable Long articleId) {
        List<ReclArticleComment> reclArticleComments = articleCommentService.list(new QueryWrapper<ReclArticleComment>()
                .eq("article_id", articleId));
        return R.ok()
                .data("reclArticleComments", reclArticleComments);
    }
}
